home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / macgambit-20-compiler-src-p2 / Thomas / msort.scm < prev    next >
Encoding:
Text File  |  1994-07-26  |  3.1 KB  |  101 lines  |  [TEXT/gamI]

  1. ; -*-Scheme-*-
  2. ; $Id: msort.scm,v 1.2 1992/09/22 20:31:33 birkholz Exp $
  3. ; $MIT-Header: msort.scm,v 14.1 88/06/13 11:47:52 GMT cph Rel $
  4. ; Copyright (c) 1988 Massachusetts Institute of Technology
  5. ; This material was developed by the Scheme project at the Massachusetts
  6. ; Institute of Technology, Department of Electrical Engineering and
  7. ; Computer Science.  Permission to copy this software, to redistribute
  8. ; it, and to use it for any purpose is granted, subject to the following
  9. ; restrictions and understandings.
  10. ; 1. Any copy made of this software must include this copyright notice
  11. ; in full.
  12. ; 2. Users of this software agree to make their best efforts (a) to
  13. ; return to the MIT Scheme project any improvements or extensions that
  14. ; they make, so that these may be included in future releases; and (b)
  15. ; to inform MIT of noteworthy uses of this software.
  16. ; 3. All materials developed as a consequence of the use of this
  17. ; software shall duly acknowledge such use, in accordance with the usual
  18. ; standards of acknowledging credit in academic research.
  19. ; 4. MIT has made no warrantee or representation that the operation of
  20. ; this software will be error-free, and MIT is under no obligation to
  21. ; provide any services, by way of maintenance, update, or otherwise.
  22. ; 5. In conjunction with products arising from the use of this material,
  23. ; there shall be no use of the name of the Massachusetts Institute of
  24. ; Technology nor of any adaptation thereof in any advertising,
  25. ; promotional, or sales literature without prior written consent from
  26. ; MIT in each case.
  27.  
  28. ;;;; Merge Sort
  29.  
  30. ; Requires an "error" procedure.
  31.  
  32. ;; Functional and unstable
  33.  
  34. (define (sort obj pred)
  35.   (define (loop l)
  36.     (if (and (pair? l) (pair? (cdr l)))
  37.     (split l '() '())
  38.     l))
  39.  
  40.   (define (split l one two)
  41.     (if (pair? l)
  42.     (split (cdr l) two (cons (car l) one))
  43.     (merge (loop one) (loop two))))
  44.  
  45.   (define (merge one two)
  46.     (cond ((null? one) two)
  47.       ((pred (car two) (car one))
  48.        (cons (car two)
  49.          (merge (cdr two) one)))
  50.       (else
  51.        (cons (car one)
  52.          (merge (cdr one) two)))))
  53.  
  54.   (cond ((or (pair? obj) (null? obj))
  55.      (loop obj))
  56.     ((vector? obj)
  57.      (sort! (vector-copy obj) pred))
  58.     (else
  59.      (error "sort: argument should be a list or vector" obj))))
  60.  
  61. ;; This merge sort is stable for partial orders (for predicates like
  62. ;; <=, rather than like <).
  63.  
  64. (define (sort! v pred)
  65.   (define (sort-internal! vec temp low high)
  66.     (if (< low high)
  67.     (let* ((middle (quotient (+ low high) 2))
  68.            (next (+ 1 middle)))
  69.       (sort-internal! temp vec low middle)
  70.       (sort-internal! temp vec next high)
  71.       (let loop ((p low) (p1 low) (p2 next))
  72.         (if (not (> p high))
  73.         (cond ((> p1 middle)
  74.                (vector-set! vec p (vector-ref temp p2))
  75.                (loop (+ 1 p) p1 (+ 1 p2)))
  76.               ((or (> p2 high)
  77.                (pred (vector-ref temp p1)
  78.                  (vector-ref temp p2)))
  79.                (vector-set! vec p (vector-ref temp p1))
  80.                (loop (+ 1 p) (+ 1 p1) p2))
  81.               (else
  82.                (vector-set! vec p (vector-ref temp p2))
  83.                (loop (+ 1 p) p1 (+ 1 p2)))))))))
  84.  
  85.   (if (not (vector? v))
  86.       (error "sort!: argument not a vector" v))
  87.  
  88.   (sort-internal! v
  89.           (vector-copy v)
  90.           0
  91.           (- (vector-length v) 1))
  92.   v)
  93.